export const TOKEN_KEY = 'ApiAuth'
export const BACKURL_KEY = 'backurl_key'
export const INVID_KEY = 'invid_key'
export const APP = 'toupiao_'

const TYPE_TO_HOME='0';
const TYPE_TO_PLAYER='1';

export const setToken = (token) => {
  localStorage.setItem(APP + TOKEN_KEY, token)
}
export const getToken = () => {
  const token = localStorage.getItem(APP + TOKEN_KEY)
  if (token && token !== '') {
    return token
  } else {
    //如果是开发环境，就自动写入测试用的token
    // if (process.env.NODE_ENV == 'development') {
    //   localStorage.setItem(APP + TOKEN_KEY,process.env.VUE_APP_DEBUG_TOKEN);
    //   return process.env.VUE_APP_DEBUG_TOKEN;
    // }
    return false
  }
}
export const getUserBackUrl = () => {
  const userBackUrl = sessionStorage.getItem(APP + BACKURL_KEY)
  if (userBackUrl && '' !== userBackUrl) {
    return userBackUrl
  } else {
    return false
  }
}
export const setUserBackUrl = (url) => {
  sessionStorage.setItem(APP + BACKURL_KEY, url)
}
export const getInvid = () => {
  const token = sessionStorage.getItem(APP + INVID_KEY)
  if (token) {
    return token
  } else {
    return false
  }
}
export const getSharePath = (type, data = null, modelName = '',mainData) => {
  // let url = window.location.protocol + '//' + window.location.host + '/' + modelName + 'home?id=' + getActiveId();
  let url = ''
  let host = window.location.host
  if (mainData.proxyHost != null) {
    host = mainData.proxyHost+'/toupiaoapi/Proxy/index?modelName='+modelName+'&type='
  }
  if (type === 'home') {
    //如果代理不为空，就走代理链接
    if (mainData.proxyHost != null) {
      url = window.location.protocol + '//' + host + TYPE_TO_HOME +'&id=' + getActiveId()
    } else {
      url = window.location.protocol + '//' + host + '/' + modelName + 'home?id=' + getActiveId()
    }
  } else if (type === 'player') {
    //如果代理不为空，就走代理链接
    if (mainData.proxyHost != null) {
      url = window.location.protocol + '//' + host + TYPE_TO_PLAYER +'&id=' + getActiveId() + '&pid=' + data
    } else {
      url = window.location.protocol + '//' + window.location.host + '/' + modelName + 'player?id=' + getActiveId() + '&pid=' + data
    }

  }
  return url
}
export const getActiveId = () => {
  let id = getQueryString('id')
  if (isNull(id)) {
    id = sessionStorage.getItem('active_id')
  } else {
    sessionStorage.setItem('active_id', id)
  }
  return id
}

export const getTrueImg = (list) => {
  let newList = []
  list.forEach(v => {
    newList.push(v.name)
  })
  return newList
}
export const setInvid = (id) => {
  sessionStorage.setItem(APP + INVID_KEY, id)
}
export const getDateDiff = (str) => {
  let sDate1 = new Date()
  let Date2 = (new Date(str))
  //把相差的毫秒数转换为天数
  return Date2.getTime() - sDate1.getTime()  //返回相差毫秒数
}

export const getQueryString = (name) => {
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null
}
/**
 * 检测手机号
 * @param phoneNum 要检查的手机号
 * @returns boolen
 */
export const checkPhoneNum = (phoneNum) => {
  let validRule = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/// 手机号码校验规则
  return validRule.test(phoneNum)
}
/**
 * 检测电话
 * @param tel 要检查的电话号
 * @returns boolen
 */
export const checkTel = (tel) => {
  return /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(tel)
}
/**
 * 检测字符是否为空
 * @param str 要检查的
 * @returns boolen
 */
export const isNull = (str) => {
  return str === undefined || str === null || str === '' || str === 'null'
}
/**
 * 判断是否是iOS
 * @returns {boolean}
 */
export const isIOS = () => {
  const u = navigator.userAgent, app = navigator.appVersion
  //const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
  const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/) //ios终端
  if (isiOS) {
    return true
  } else {
    return false
  }
}

